package com.xbts.screen.monitor.domain.request;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbts.screen.monitor.domain.constant.MonitorConstant;
import com.xbts.screen.monitor.domain.response.DeviceListResponse;
import com.xbts.screen.monitor.domain.token.OauthConfigBaseInfo;
import com.xbts.screen.monitor.domain.util.HttpURLConnectionUtil;
import com.xbts.screen.monitor.domain.util.HttpsURLConnectionUtil;
import lombok.Data;

import java.util.List;

/**
 * 获取实时流地址
 * **/
@Data
public class DeviceListRequest extends BaseRequest<DeviceListResponse> {
    private Long pageNum = 1L;
    private Long pageSize = 12L;
    private String deviceCategory;
    private String deviceType;
    private String ownerCode;
    private Integer isOnline;
    private List<Integer> unitTypeList;

    public DeviceListRequest(){}

    public DeviceListRequest(OauthConfigBaseInfo oauthConfigBaseInfo){
        super(oauthConfigBaseInfo, MonitorConstant.HTTP_POST, oauthConfigBaseInfo.getHttpConfigInfo().getPrefixUrl() + MonitorConstant.DEVICE_LIST, true);
    }

    @Override
    public DeviceListResponse doAction() {
        //设置请求体
        setBody();
        //发送请求
        String res = isEnableHttpTest() ? HttpURLConnectionUtil.doPost(this.getUrl(), this.getBody(), this.getHeader()) : HttpsURLConnectionUtil.doPost(this.getUrl(), this.getBody(), this.getHeader());
        //获取响应
        DeviceListResponse deviceListResponse = JSON.parseObject(res, DeviceListResponse.class);

        return deviceListResponse;
    }

    public void setBody(){
        //设置请求参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceCategory",deviceCategory);
        jsonObject.put("deviceType", deviceType);
        jsonObject.put("ownerCode", ownerCode);
        jsonObject.put("isOnline", isOnline);
        jsonObject.put("unitTypeList", unitTypeList);
        jsonObject.put("pageNum", pageNum);
        jsonObject.put("pageSize", pageSize);
        this.setBody(JSON.toJSONString(jsonObject));
    }

    public boolean isEnableHttpTest(){
        return super.getOauthConfigBaseInfo().getHttpConfigInfo().isEnableHttpTest();
    }

    public void setParam(DeviceListRequest deviceListRequest){
        this.deviceType = deviceListRequest.getDeviceType();
        this.deviceCategory = deviceListRequest.getDeviceCategory();
        this.ownerCode = deviceListRequest.getOwnerCode();
        this.isOnline = deviceListRequest.getIsOnline();
        this.unitTypeList = deviceListRequest.getUnitTypeList();
    }

}
